﻿// OscillatorControl.xaml.cs (c) Copyright 2009, Mike Hodnick : www.hodnick.com

using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Kindohm.KSynth.Library;
using System.Windows.Shapes;
using System.Windows.Input;

namespace Kindohm.KSynth.AudioCanvas
{
    public partial class OscillatorControl : UserControl, ICanvasControl
    {
        Color oldColor;
        Oscillator oscillator;
        ObservableCollection<WaveForm> waveForms;
        bool frequencyModulatorIsVisible;
        bool amplitudeModulatorIsVisible;
        bool envelopeIsVisible;

        public OscillatorControl()
        {
            InitializeComponent();
            this.waveForms = new ObservableCollection<WaveForm>(){
                 WaveForm.Sine, WaveForm.Saw, WaveForm.Square, WaveForm.Triangle};
            this.waveFormList.ItemsSource = this.waveForms;
            this.waveFormList.SelectedIndex = 0;

            this.Loaded += new RoutedEventHandler(OscillatorControl_Loaded);
        }


        public Oscillator Oscillator
        {
            get { return this.oscillator; }
            set
            {
                this.oscillator = value;
                this.oscillator.SetFrequency(this.frequencyUpDown, NumericUpDown.ValueProperty);
                this.oscillator.WaveForm(this.waveFormList, ComboBox.SelectedItemProperty);
            }
        }

        void OscillatorControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            this.MouseEnter += new MouseEventHandler(OscillatorControl_MouseEnter);
            this.MouseLeave += new MouseEventHandler(OscillatorControl_MouseLeave);
        }

        void OscillatorControl_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.HideMenu();
        }

        void OscillatorControl_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.ShowMenu();
        }

        public TranslateTransform Translation
        {
            get { return this.translation; }
        }

        void ShowMenu()
        {
            this.menuCanvas.Visibility = Visibility.Visible;
        }

        void HideMenu()
        {
            this.menuCanvas.Visibility = Visibility.Collapsed;
        }



        private void Path_MouseEnter(object sender, MouseEventArgs e)
        {

            Path path = sender as Path;
            SolidColorBrush brush = path.Fill as SolidColorBrush;
            this.oldColor = brush.Color;
            if (path == deletePath)
                brush.Color = Color.FromArgb(255, 150, 0, 0);
            else
                brush.Color = Color.FromArgb(255, 66, 66, 66);
        }

        private void Path_MouseLeave(object sender, MouseEventArgs e)
        {
            Path path = sender as Path;
            SolidColorBrush brush = path.Fill as SolidColorBrush;
            brush.Color = oldColor;
        }

        private void AmplitudePath_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.amplitudeModulatorIsVisible)
            {
                this.amplitudeModulatorIsVisible = false;
                if (this.HideAmplitudeModulateButtonClicked != null)
                    this.HideAmplitudeModulateButtonClicked(this, EventArgs.Empty);
            }
            else
            {
                this.amplitudeModulatorIsVisible = true;
                if (this.ShowAmplitudeModulateButtonClicked != null)
                    this.ShowAmplitudeModulateButtonClicked(this, EventArgs.Empty);
            }
        }

        private void FrequencyPath_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.frequencyModulatorIsVisible)
            {
                this.frequencyModulatorIsVisible = false;
                if (this.HideFrequencyModulateButtonClicked != null)
                    this.HideFrequencyModulateButtonClicked(this, EventArgs.Empty);
            }
            else
            {
                this.frequencyModulatorIsVisible = true;
                if (this.ShowFrequencyModulateButtonClicked != null)
                    this.ShowFrequencyModulateButtonClicked(this, EventArgs.Empty);
            }
        }

        private void EnvelopePath_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.envelopeIsVisible)
            {
                this.envelopeIsVisible = false;
                if (this.HideEnvelopeButtonClicked != null)
                    this.HideEnvelopeButtonClicked(this, EventArgs.Empty);
            }
            else
            {
                this.envelopeIsVisible = true;
                if (this.ShowEnvelopeButtonClicked != null)
                    this.ShowEnvelopeButtonClicked(this, EventArgs.Empty);
            }
        }

        private void DeletePath_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.DeleteButtonClicked != null)
                this.DeleteButtonClicked(this, EventArgs.Empty);

        }

        public event EventHandler ShowFrequencyModulateButtonClicked;
        public event EventHandler HideFrequencyModulateButtonClicked;
        public event EventHandler ShowAmplitudeModulateButtonClicked;
        public event EventHandler HideAmplitudeModulateButtonClicked;
        public event EventHandler ShowEnvelopeButtonClicked;
        public event EventHandler HideEnvelopeButtonClicked;
        public event EventHandler DeleteButtonClicked;

    }
}
